<?php
/**
 * @file bvlgari.php
 * @author LinLin
 * @date 2016年3月23日 下午12:03:17
 * @brief 
 */
namespace haibao\cms\data\control\bvlgari;

class BvlgariController extends \haibao\cms\data\BaseMysql {

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\Base');
    }
    
    public function getArticlesByTagId($tagId){
        list($articleIds,$articleInfo) = $this->getArticleInfoByTagId($tagId);
        $articleInfo = $this->getTagArticleInfo($articleIds,$articleInfo);
        $article = new \haibao\cms\common\ArticleContent();
        foreach ($articleInfo as $key=>$model){
            $staticUrl = $article->replaceImageLink($model['PicUrl'], 300, 300, 100, 0);
            $articleInfo[$key]['ImageUrl'] = $article->replaceImgUrlCDNLink($staticUrl);
        }
        if($articleInfo){
            $articleCommon = new \haibao\cms\web\common\PublishArticle();
            foreach($articleInfo as $key=>$info){
                $articleInfo[$key]['ArticleUrl'] = \haibao\cms\business\control\Base::getArticleUrl($info['PublishId'],$info['ParentId']);
                $articleInfo[$key]['TagUrl'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_WEDDING).'/wedding/'.$info['TagId'].'/';
            }
        }
        return $articleInfo;
    }
    
    /**
     * 根据TagId获取文章信息
     */
    public function getArticleInfoByTagId($tagId){
        $sql = 'SELECT
                	a.Id,
                	a.Title,
                	a.ShortTitle,
                	a.PublishTime,
                	a.PublishId,
                	b.ParentId,
                    b.TagId
                FROM
                	Article AS a
                LEFT JOIN ArticleTag AS b ON a.Id = b.ArticleId
                WHERE
                	a. STATUS = 0
                AND a.Published = 1
                AND b.ParentId = %s
                AND b.IsPrimaiy = 1
                AND b.TagId <> 10871
                GROUP BY
                	a.Id
                ORDER BY
                	a.PublishTime DESC
                LIMIT 9';
        $result = $this->query($sql, array($tagId));
        $articleInfo = $articleIds = $articleTagInfo = array();
        while($row = $result->fetch_assoc()){
            $articleInfo[] = $row;
            array_push($articleIds,$row['Id']);
        }
        $articleData = new \haibao\cms\data\Article();
        $articleProInfoOne = $articleData->getProImageFigure($articleIds,4);
        $articleProInfoTwo = $articleData->getProImageFigure($articleIds,0);
        foreach($articleInfo as $key=>$val){
            if(isset($articleProInfoOne[$val['Id']])){
                $articleInfo[$key]['PicUrl'] = $articleProInfoOne[$val['Id']]['PicUrl'];
            }else{
                $articleInfo[$key]['PicUrl'] = '';
            }
        }
        foreach($articleInfo as $key=>$val){
            if(isset($articleProInfoTwo[$val['Id']]) && $articleInfo[$key]['PicUrl'] == ''){
                $articleInfo[$key]['PicUrl'] = $articleProInfoTwo[$val['Id']]['PicUrl'];
            }
        }
        return array($articleIds,$articleInfo);
    }
    
    public function getTagArticleInfo($articleIds,$articleInfo){
        $articleTagInfo = array();
        foreach($articleInfo as $key=>$val){
            array_push($articleTagInfo,$val['TagId']);
        }
        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE );
        $tagIdStr = preg_replace('/\d+/', '%s', implode(',', $articleTagInfo));
        $sql = 'select tag_id,tag_name from tag where tag_id in ('.$tagIdStr.')';
        $tagResult = $this->query($sql, $articleTagInfo);
        while($row = $tagResult->fetch_assoc()){
            $tagInfo[$row['tag_id']] = $row['tag_name'];
        }
        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
        foreach($articleInfo as $key=>$val){
            if(isset($tagInfo[$val['TagId']])){
                $articleInfo[$key]['TagName'] = $tagInfo[$val['TagId']];
            }
        }
        return $articleInfo;
    }
} 